﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta><style>/*<![CDATA[*/

table{border: 1px solid gray;}
td{border: 1px dotted gray;}
p{margin: 3px 0 3px 0; padding: 0;}
#ID_Footer{font-size: small; font-style: italic; text-align: right; margin-top: 4em; padding-top: 4px; border-top: 2px solid gray;}

/*]]>*/</style><title>02. ThinkPHP 3.1.2 MVC模式和URL访问</title></head><body>
<div><span style="font-family: 微软雅黑; font-size: 9pt; color: #000000; line-height: 140%">本节课大纲：</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; line-height: 140%">一、什么是MVC &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //了解</span></div>
<div><span style="font-family: 微软雅黑; font-size: 9pt; line-height: 140%"> &nbsp; &nbsp;M - Model 编写Model类，对数据进行操作</span></div>
<div><span style="font-family: 微软雅黑; font-size: 9pt; line-height: 140%"> &nbsp; &nbsp;V - View，编写html文件，页面呈现</span></div>
<div><span style="font-family: 微软雅黑; font-size: 9pt; line-height: 140%"> &nbsp; &nbsp;C - Controller，编写类文件(UserAction.Class.php)</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; line-height: 140%">二、ThinkPHP的MVC的特点 &nbsp; &nbsp; &nbsp; //了解</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; line-height: 140%">三、ThinkPHP的MVC对应的目录 &nbsp; //了解</span></div>
<div><span style="font-family: 微软雅黑; font-size: 9pt; line-height: 140%"> &nbsp; &nbsp;M 项目目录/应用目录</span><span style="font-family: 微软雅黑; font-size: 9pt; font-weight: bold; color: #ff0000; line-height: 140%">/lib/Model</span></div>
<div><span style="font-family: 微软雅黑; font-size: 9pt; line-height: 140%"> &nbsp; &nbsp;V </span><span style="font-family: 微软雅黑; font-size: 9pt; font-weight: bold; line-height: 140%">项目目录/应用目录/</span><span style="font-family: 微软雅黑; font-size: 9pt; font-weight: bold; color: #ff0000; line-height: 140%">Tpl</span></div>
<div><span style="font-family: 微软雅黑; font-size: 9pt; line-height: 140%"> &nbsp; &nbsp;C 项目目录/应用目录</span><span style="font-family: 微软雅黑; font-size: 9pt; font-weight: bold; color: #ff0000; line-height: 140%">/lib/Action</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; color: #000000; line-height: 140%">四、url访问C &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//了解</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; line-height: 140%">五、url的4种访问方式 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//重点！</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp;1. </span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 140%">PATHINFO</span><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> 模式 -- 重点!!!!</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 190%"> &nbsp; &nbsp; &nbsp;</span><a href="http://" style="font-family: 微软雅黑; font-size: 10pt; text-decoration: underline; color: #0000ff">http://</a><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 190%">域名/项目名/入口文件/模块名/方法名/键1/值1/键2/值2</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp;2. 普通模式</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 190%"> &nbsp; &nbsp; &nbsp;</span><a href="http://" style="font-family: 微软雅黑; font-size: 10pt; text-decoration: underline; color: #0000ff">http://</a><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 190%">域名/项目名/入口文件?</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; color: #000000; line-height: 190%">m</span><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 190%">=模块名&amp;</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; color: #000000; line-height: 190%">a</span><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 190%">=方法名&amp;键1=值1&amp;键2=值2</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 190%"> &nbsp; &nbsp;3. REWRITE模式</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp;4. 兼容模式</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; </span><a href="http://" style="font-family: 微软雅黑; font-size: 10pt; text-decoration: underline; color: #0000ff">http://</a><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 140%">域名/项目名/入口文件?</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; color: #000000; line-height: 140%">s</span><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 140%">=模块名/方法名/键1/值1/键2/值2</span></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 9pt; line-height: 140%">================================Leo==============================================</span></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; line-height: 190%">1. 模块 &amp; 方法</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp;访问应用的3种等同的URL：</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;</span><a href="http://localhost/thinkphp" style="font-family: 微软雅黑; font-size: 10pt; text-decoration: underline; color: #0000ff">http://localhost/thinkphp</a></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;</span><a href="http://localhost/thinkphp/index.php" style="font-family: 微软雅黑; font-size: 10pt; text-decoration: underline; color: #0000ff">http://localhost/thinkphp/index.php</a></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 190%"> &nbsp; &nbsp; &nbsp; &nbsp;</span><a href="http://localhost/thinkphp/index.php/Index/index" style="font-family: 微软雅黑; font-size: 10pt; text-decoration: underline; color: #0000ff">http://localhost/thinkphp/index.php/Index/index</a><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 190%"> &nbsp; &nbsp; &nbsp; &nbsp;//Index模块（C）下的index方法</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp;</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 140%">Index模块</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 190%"> &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 190%">对应：Lib/Action/IndexAction.class.php 文件的 IndexAction类</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp;</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 140%">index方法</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%">对应 IndexAction.class.php中的IndexAction类的index方法</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 190%"> &nbsp; &nbsp; &nbsp; &nbsp;还可以对应其它方法，对应地址栏地址：</span><a href="http://localhost/thinkphp/index.php/Index/show" style="font-family: 微软雅黑; font-size: 10pt; text-decoration: underline; color: #0000ff">http://localhost/thinkphp/index.php/Index/show</a><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 190%"> ，即在IndexAction类下的show方法</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp;</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 140%">传值：</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;通过URL地址写入，</span><a href="http://" style="font-family: 微软雅黑; font-size: 10pt; text-decoration: underline; color: #0000ff">http://</a><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 140%">域名/项目名/入口文件/模块名/方法名/键1/值1/键2/值2</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><a href="http://localhost/thinkphp/index.php/Index/show/name/Tony/age/18" style="font-family: 微软雅黑; font-size: 10pt; text-decoration: underline; color: #0000ff">http://localhost/thinkphp/index.php/Index/show/name/Tony/age/18</a></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 190%"> &nbsp; &nbsp; &nbsp; &nbsp;再在模块方法中通过</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 190%">$_Get[&apos;name&apos;]</span><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 190%">形式使用。</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp;</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 140%">rewrite模式</span><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%">，省略index.php，即</span><a href="http://localhost/thinkphp/Index/index" style="font-family: 微软雅黑; font-size: 10pt; text-decoration: underline; color: #0000ff">http://localhost/thinkphp/Index/index</a></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;需要使apache服务器支持rewrite模式</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a. 在Apache配置文件httpd.conf中，去掉rewrite行的注释，重启服务</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b. 在入口文件同目录下（项目目录下），创建.htaccess文件，里面写入rewrite规则。</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; line-height: 190%">2. 配置修改</span></div>
<div><span style="font-family: 微软雅黑; font-size: 11pt; line-height: 190%"> &nbsp; &nbsp; &nbsp; &nbsp;路径为：</span><span style="font-family: 微软雅黑; font-size: 11pt; font-weight: bold; line-height: 190%">应用/Conf/config.php</span></div>
<div><span style="font-family: 微软雅黑; font-size: 11pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;修改地址中键值分隔符：即</span></div>
<div><span style="font-family: 微软雅黑; font-size: 11pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><a href="http://localhost/thinkphp/index.php/Index-show-name-Tony-age-18" style="font-family: 微软雅黑; font-size: 11pt; text-decoration: underline; color: #0000ff">http://localhost/thinkphp/index.php/Index-show-name-Tony-age-18</a></div>
<div><span style="font-family: 微软雅黑; font-size: 11pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;&lt;?php</span></div>
<div><span style="font-family: 微软雅黑; font-size: 11pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return array{</span></div>
<div><span style="font-family: 微软雅黑; font-size: 11pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;</span><span style="font-family: 微软雅黑; font-size: 11pt; font-weight: bold; color: #ff0000; line-height: 140%">PATH_INFO_DEPR</span><span style="font-family: 微软雅黑; font-size: 11pt; color: #000000; line-height: 140%">&quot; =&gt;&quot;-&quot;, //修改URL分隔符</span></div>
<div><span style="font-family: 微软雅黑; font-size: 11pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span><span style="font-family: 微软雅黑; font-size: 11pt; font-weight: bold; color: #ff0000; line-height: 140%">;</span></div>
<div><span style="font-family: 微软雅黑; font-size: 11pt; line-height: 190%"> &nbsp; &nbsp; &nbsp; &nbsp;?&gt;</span></div>
<div><span style="font-family: 微软雅黑; font-size: 11pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;修改后不能即时生效问题：删除缓存</span></div>
<div><span style="font-family: 微软雅黑; font-size: 11pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;项目/Runtime文件夹</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; line-height: 140%">3. 开启调试模式，不缓存</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;在项目index.php入口文件中：</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; color: #ff0000; line-height: 140%">define(&apos;APP_DEBUG&apos;, true);</span></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 9pt; line-height: 140%"> &nbsp; &nbsp; &nbsp;</span></div>
<div><br /></div>
<div><br /></div><script type="text/javascript" language="javascript" src="jquery.js"></script><script type="text/javascript" language="javascript" src="itemlink.js"></script></body></html>